﻿
@using IOA.MES.DataAccess.Enum
@{
    ViewBag.Title = "实时产能";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
    var siteTypes = ViewBag.SiteTypes as List<KeyValuePair<int, string>>;
    var workshops = ViewBag.Workshops as List<IOA.MES.DataAccess.VModel.VWorkshop>;
    var startTime = DateTime.Now.TimeOfDay < new TimeSpan(8, 0, 0) ? DateTime.Today.AddDays(-1).AddHours(20) : (DateTime.Now.TimeOfDay < new TimeSpan(20, 0, 0) ? DateTime.Today.AddHours(8) : DateTime.Today.AddHours(20));
    var endTime = DateTime.Now.TimeOfDay < new TimeSpan(8, 0, 0) ? DateTime.Today.AddHours(8) : (DateTime.Now.TimeOfDay < new TimeSpan(20, 0, 0) ? DateTime.Today.AddHours(20) : DateTime.Today.AddDays(1).AddHours(8));
}
@section Contents{
    <style>
        .layui-form-label {
            width: auto;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkShop">车间：</label>
                        <div class="layui-input-inline">
                            <select id="selWorkshops1" lay-filter="selWorkshops">
                                @foreach (var item in workshops)
                                {
                                    <option value="@item.WorkshopCode" @(workshops.IndexOf(item) == 0 ? " selected " : "")>@item.WorkshopName</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.ProduceTime">生产时间：</label>
                        <div class="layui-input-inline" style="width: 310px;">
                            <input type="text" id="txtRangeTime1" value="@(startTime.ToString("yyyy-MM-dd HH:mm:ss") + " 至 " + endTime.ToString("yyyy-MM-dd HH:mm:ss"))" autocomplete="off" class="layui-input rangetime">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch1" class="layui-btn layuiadmin-btn-list" type="button">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
            <div class="layui-card-body" id="chart1" style="height:500px;"></div>
        </div>
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkShop">车间：</label>
                        <div class="layui-input-inline">
                            <select id="selWorkshops2" lay-filter="selWorkshops">
                                @foreach (var item in workshops)
                                {
                                    <option value="@item.WorkshopCode" @(workshops.IndexOf(item) == 0 ? " selected " : "")>@item.WorkshopName</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.SiteType">站点类型：</label>
                        <div class="layui-input-inline">
                            <select id="selSiteTypes2" lay-filter="selSiteTypes">
                                @foreach (var item in siteTypes)
                                {
                                    <option value="@item.Key">@item.Value</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.ProduceTime">生产时间：</label>
                        <div class="layui-input-inline" style="width: 310px;">
                            <input type="text" id="txtRangeTime2" value="@(startTime.ToString("yyyy-MM-dd HH:mm:ss") + " 至 " + endTime.ToString("yyyy-MM-dd HH:mm:ss"))" autocomplete="off" class="layui-input rangetime">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch2" class="layui-btn layuiadmin-btn-list" type="button">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
            <div class="layui-card-body" id="chart2" style="height:500px;"></div>
        </div>
    </div>
</script>

@section Scripts{
    <script src="/res/echarts/echarts.min.js"></script>
    <script src="/res/echarts/macarons.js"></script>
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });
        var option1 = {
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'shadow'
                }
            },
            legend: {
                data: ['A线']
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis: {
                type: 'category',
                data: ['焊接']
            },
            yAxis: {
                type: 'value'
            },
            series: [
                {
                    name: 'A线',
                    type: 'bar',
                    stack: '总量',
                    label: {
                        show: true,
                        position: 'inside'
                    },
                    data: [320, 302, 301, 334, 390, 330, 320]
                }
            ]
        },
            option2 = {
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    }
                },
                legend: {
                    data: ['A线']
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: {
                    type: 'category',
                    data: ['焊接']
                },
                yAxis: {
                    type: 'value'
                },
                series: [
                    {
                        name: 'A线',
                        type: 'bar',
                        stack: '总量',
                        label: {
                            show: true,
                            position: 'inside'
                        },
                        data: [320, 302, 301, 334, 390, 330, 320]
                    }
                ]
            };
        var myChart1 = echarts.init(document.getElementById('chart1'), 'macarons'),
            myChart2 = echarts.init(document.getElementById('chart2'), 'macarons');
        setTimeout(function () {
            initLayui();
        }, 50);

        function initLayui() {
            layui.use(['table', 'layer', 'form', 'laydate', 'element'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    form = layui.form,
                    laydate = layui.laydate;
                setTimeout(function () {
                    laydate.render({
                        elem: '.rangetime'
                        , type: 'datetime'
                        , rigger: 'click'
                        , range: '至'
                    });
                }, 100);

                $("#btnSearch1").click(function () {
                    queryCapacity();
                });
                queryCapacity();
                function queryCapacity() {
                    var workShopCode = $("#selWorkshops1").val(),
                        rangeTime = $("#txtRangeTime1").val();
                    var startTime = rangeTime.split('至')[0].trim(),
                        endTime = rangeTime.split('至')[1].trim();

                    var loadId = layui.layer.load();
                    $.ajax({
                        url: "/Report/GetRealTimeCapacity",
                        data: {
                            workShopCode: workShopCode,
                            startTime: startTime,
                            endTime: endTime
                        },
                        complete: function () {
                            layui.layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layui.layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            option1.legend.data = res.Data.Lines;
                            option1.legend.data.push("总和");
                            option1.xAxis.data = res.Data.SiteTypes;
                            var totalArray = new Array().concat(res.Data.LineCapacities.length <= 0 ? new Array() : res.Data.LineCapacities[0].Value);
                            var series = new Array();
                            for (var i = 0; i < res.Data.LineCapacities.length; i++) {
                                series.push({
                                    name: res.Data.LineCapacities[i].Key,
                                    type: 'bar',
                                    stack: '总量',
                                    label: {
                                        show: true,
                                        position: 'inside'
                                    },
                                    data: res.Data.LineCapacities[i].Value
                                });
                                if (i > 0) {
                                    for (var j = 0; j < totalArray.length; j++) {
                                        totalArray[j] = totalArray[j] + res.Data.LineCapacities[i].Value[j];
                                    }
                                }
                            }
                            series.push({
                                name: "总和",
                                type: 'line',
                                stack: '总量',
                                lineStyle: {
                                    opacity: 0,
                                },
                                label: {
                                    show: true,
                                    textStyle: {
                                        color: '#009688'
                                    }
                                },
                                data: totalArray
                            });
                            option1.series = series;
                            myChart1.setOption(option1);
                        }
                    });
                }

                $("#btnSearch2").click(function () {
                    queryHourCapacity();
                });
                queryHourCapacity();
                function queryHourCapacity() {
                    var workShopCode = $("#selWorkshops2").val(),
                        siteType = parseInt($("#selSiteTypes2").val()),
                        rangeTime = $("#txtRangeTime2").val();
                    var startTime = rangeTime.split('至')[0].trim(),
                        endTime = rangeTime.split('至')[1].trim();

                    var loadId = layui.layer.load();
                    $.ajax({
                        url: "/Report/GetHourCapacity",
                        data: {
                            workShopCode: workShopCode,
                            siteType: siteType,
                            startTime: startTime,
                            endTime: endTime
                        },
                        complete: function () {
                            layui.layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layui.layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            option2.legend.data = res.Data.Lines;
                            option2.legend.data.push("总和");
                            option2.xAxis.data = res.Data.Hours;
                            var totalArray = new Array().concat(res.Data.LineCapacities.length <= 0 ? new Array() : res.Data.LineCapacities[0].Value);
                            var series = new Array();
                            for (var i = 0; i < res.Data.LineCapacities.length; i++) {
                                series.push({
                                    name: res.Data.LineCapacities[i].Key,
                                    type: 'bar',
                                    stack: '总量',
                                    label: {
                                        show: true,
                                        position: 'inside'
                                    },
                                    data: res.Data.LineCapacities[i].Value
                                });
                                if (i > 0) {
                                    for (var j = 0; j < totalArray.length; j++) {
                                        totalArray[j] = totalArray[j] + res.Data.LineCapacities[i].Value[j];
                                    }
                                }
                            }
                            series.push({
                                name: "总和",
                                type: 'line',
                                stack: '总量',
                                lineStyle: {
                                    opacity: 0,
                                },
                                label: {
                                    show: true,
                                    textStyle: {
                                        color: '#009688'
                                    }
                                },
                                data: totalArray
                            });
                            option2.series = series;
                            myChart2.setOption(option2);
                        }
                    });
                }
            });
        }
    </script>
}
